import dayjs from 'dayjs';

export const isCurrentYear = (date: string) => {
    // 获取当前年份
    const currentYear = dayjs().year();
    // 获取传入日期的年份
    const targetYear = dayjs(date).year();
    // 比较两个年份是否相同
    return currentYear === targetYear;
}

export const getEmailName = (email?: string) => {
  if (email) {
      let str = email.split('@')[0];
      if (str.startsWith('<')) {
        return str.slice(1);
      }

      return str;
  }
  return '';
};

// export const charToDarkColor = (char: string) => {
//   // 将字符转换为 Unicode 码值
//   let code = char.charCodeAt(0);
//   // 根据 Unicode 码值和一个基数来生成颜色值，使不同字符的颜色差距较大
//   const base = 0x100000;
//   let color = '#' + ('000000' + ((code * base) % 0x1000000).toString(16)).slice(-6);
//   return color;
// }

export const charToDarkColor = (char: string) => {
  const baseColors = [
    '#105039', '#B53471', '#9980FA', '#F5891D', '#CE0525'
  ];
  let code = char.charCodeAt(0) % baseColors.length;
  let color = baseColors[code];
  
  // 将十六进制颜色转换为 RGB 格式
  let r = parseInt(color.slice(1, 3), 16);
  let g = parseInt(color.slice(3, 5), 16);
  let b = parseInt(color.slice(5, 7), 16);
  
  // 使颜色变浅（通过向白色 #FFFFFF 靠近）
  const lightnessFactor = 0.3; // 浅色因子，可根据需要调整
  r = Math.min(255, Math.floor(r + (255 - r) * lightnessFactor));
  g = Math.min(255, Math.floor(g + (255 - g) * lightnessFactor));
  b = Math.min(255, Math.floor(b + (255 - b) * lightnessFactor));
  
  // 转换回十六进制颜色
  return '#' + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1).toUpperCase();
}


/**
 * 参数处理
 * @param {*} params  参数
 */
export function tansParams(params: { [x: string]: any }) {
  let result = ''
  for (const propName of Object.keys(params)) {
    const value = params[propName]
    const part = `${encodeURIComponent(propName)}=`
    if (value !== null && value !== '' && typeof (value) !== 'undefined') {
      if (typeof value === 'object') {
        for (const key of Object.keys(value)) {
          if (value[key] !== null && value[key] !== '' && typeof (value[key]) !== 'undefined') {
            const params = `${propName}[${key}]`
            const subPart = `${encodeURIComponent(params)}=`
            result += `${subPart + encodeURIComponent(value[key])}&`
          }
        }
      }
      else {
        result += `${part + encodeURIComponent(value)}&`
      }
    }
  }
  return result
}